﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace JYStockAnalyser.DA
{
    public class ORMapper
    {
        protected SqlConnection CreateConnection()
        {

            SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conString"]);
            return cn;
        }

        protected SqlDataReader Excute_Reader(string sql)
        {
            SqlConnection cn = CreateConnection();
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        protected int ExecuteNonQuery(string sql)
        {
            int result = 0;
            using (SqlConnection cn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(sql, cn);
                cn.Open();
                result = cmd.ExecuteNonQuery();
            }
            return result;
        }

        public string TranslateDTToString(DateTime? dt)
        {
            if (!dt.HasValue) return null;
            return dt.Value.Year.ToString() + dt.Value.Month.ToString("00") + dt.Value.Day.ToString("00");
        }

        public DateTime? TranslateStringToDT(string str)
        {
            if (str == null || str == string.Empty) return null;
            return new DateTime(int.Parse(str.Substring(0, 4)), int.Parse(str.Substring(4, 2)), int.Parse(str.Substring(6, 2)));
        }



        public bool IsEmpty(string str)
        {
            return String.IsNullOrEmpty(str);
        }

        public bool IsEmpty(Guid id)
        {
            return (id == null || id.Equals(Guid.Empty));
        }

        public Guid? GetReaderGuid(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return null;
            }
            else
            {
                return reader.GetGuid(colIndex);
            }
        }

        public Guid GetReaderGuidValue(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return Guid.Empty;
            }
            else
            {
                return reader.GetGuid(colIndex);
            }
        }

        public string GetReaderString(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return string.Empty;
            }
            else
            {
                return reader.GetString(colIndex);
            }
        }

        public int GetReaderInt32Value(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return 0;
            }
            else
            {
                return reader.GetInt32(colIndex);
            }
        }

        public int? GetReaderInt32(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return null;
            }
            else
            {
                return reader.GetInt32(colIndex);
            }
        }

        public decimal GetReaderDecimalValue(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(reader.GetValue(colIndex));
            }
        }

        public decimal? GetReaderDecimal(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return null;
            }
            else
            {
                return GetReaderDecimalValue(reader, colIndex);
            }
        }

        public DateTime GetReaderDateTimeValue(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return DateTime.MinValue;
            }
            else
            {
                return reader.GetDateTime(colIndex);
            }
        }

        public DateTime? GetReaderDateTime(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return null;
            }
            else
            {
                string date = reader.GetString(colIndex);
                return new DateTime(int.Parse(date.Substring(0, 4)), int.Parse(date.Substring(4, 2)), int.Parse(date.Substring(6, 2)));
            }
        }

        public string GetDateTimeString(DateTime? dt)
        {
            if (!dt.HasValue)
                return null;
            else
            {
                return dt.Value.Year.ToString() + dt.Value.Month.ToString("00") + dt.Value.Day.ToString("00");
            }
        }

        public bool? GetReaderBoolean(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return null;
            }
            else
            {
                return reader.GetBoolean(colIndex);
            }
        }

        public bool GetReaderBooleanValue(IDataReader reader, int colIndex)
        {
            if (reader.IsDBNull(colIndex))
            {
                return false;
            }
            else
            {
                return reader.GetBoolean(colIndex);
            }
        }

        public byte[] GetReaderByteArray(IDataReader reader, int index)
        {
            return (byte[])reader.GetValue(index);
        }

    }
}
